﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Web.UI;
using System.Xml;

namespace QT.IO
{
    /// <summary>
    /// XML操作类
    /// </summary>
    public class XML
    {
        #region Attributes

        /// <summary>
        /// XML文件的服务器路径
        /// </summary>
        private static string t_xPath = "";
        /// <summary>
        /// XML文件的服务器路径
        /// </summary>
        public static string xPath
        {
            set { t_xPath = value.Trim(); }
            get { return t_xPath.Trim(); }
        }

        #endregion

        #region 构造函数

        /// <summary>
        /// 不带参方法
        /// </summary>
        public XML()
        {
            t_xPath = new System.Web.UI.Page().Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["XMLPATH"].ToString());
        }

        /// <summary>
        /// 带参方法
        /// </summary>
        /// <param name="path">XML文件的服务器路径</param>
        public XML(string path)
        {
            t_xPath = path;
        }

        #endregion

        #region XML操作
        
        /// <summary>
        /// 读取子节点
        /// </summary>
        /// <param name="node">节点</param>
        /// <returns>string</returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Read("/Node")
         ************************************************/
        public string[] GetNodes(string node)
        {
            string[] value = null;
            int i = 0;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xPath);
                XmlNodeList xnl = doc.SelectNodes(node);
                foreach (XmlNode xn in xnl)
                {
                    value[i] = node + "/" + xn.Name;
                    i++;
                }
            }
            catch { }

            return value;
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时返回该属性值，否则返回串联值</param>
        /// <returns>string</returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Read("/Node", "")
         * XmlHelper.Read("/Node/Element[@Attribute='Name']", "Attribute")
         ************************************************/
        public string Read(string node, string attribute)
        {
            string value = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xPath);
                XmlNode xn = doc.SelectSingleNode(node);
                value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
            }
            catch { }
            return value;
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时返回该属性值，否则返回串联值</param>
        /// <returns>string</returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Read("/Node", "")
         * XmlHelper.Read("/Node/Element[@Attribute='Name']", "Attribute")
         ************************************************/
        public string[] MultiRead(string node, string attribute)
        {
            string[] value = null;
            int i = 0;
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xPath);
                XmlNodeList xnl = doc.SelectNodes(node);

                value = new string[xnl.Count];
                foreach (XmlNode xn in xnl)
                {
                    value[i] = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
                    i++;
                }
            }
            catch { }
            return value;
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="element">元素名，非空时插入新元素，否则在该元素中插入属性</param>
        /// <param name="attribute">属性名，非空时插入该元素属性值，否则插入元素值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Insert("/Node", "Element", "", "Value")
         * XmlHelper.Insert("/Node", "Element", "Attribute", "Value")
         * XmlHelper.Insert("/Node", "", "Attribute", "Value")
         ************************************************/
        public bool Insert(string node, string element, string attribute, string value)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xPath);
                XmlNode xn = doc.SelectSingleNode(node);
                if (element.Equals(""))
                {
                    if (!attribute.Equals(""))
                    {
                        XmlElement xe = (XmlElement)xn;
                        xe.SetAttribute(attribute, value);
                    }
                }
                else
                {
                    XmlElement xe = doc.CreateElement(element);
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    xn.AppendChild(xe);
                }
                doc.Save(xPath);
            }
            catch 
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时修改该节点属性值，否则修改节点值</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Insert("/Node", "", "Value")
         * XmlHelper.Insert("/Node", "Attribute", "Value")
         ************************************************/
        public bool Update(string node, string attribute, string value)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xPath);
                XmlNode xn = doc.SelectSingleNode(node);
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals(""))
                    xe.InnerText = value;
                else
                    xe.SetAttribute(attribute, value);
                doc.Save(xPath);
            }
            catch 
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="node">节点</param>
        /// <param name="attribute">属性名，非空时删除该节点属性值，否则删除节点值</param>
        /// <returns></returns>
        /**************************************************
         * 使用示列:
         * XmlHelper.Delete("/Node", "")
         * XmlHelper.Delete("/Node", "Attribute")
         ************************************************/
        public bool Delete(string node, string attribute)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xPath);
                XmlNode xn = doc.SelectSingleNode(node);
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn);
                else xe.RemoveAttribute(attribute);
                doc.Save(xPath);
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion 
    }
}
